<template>
  <div class="login-con">
    <div class="login-con-title">
      <span>商品（服务）供应商随机抽选系统</span>
    </div>
    <div class="login-con-logo" />
    <div class="login-con-box">
      <el-form>
        <h2>欢迎登录</h2>
        <div class="login-con-box-item">
          <i class="lcbi-user" />
          <el-input
            v-model="loginForm.userName"
            placeholder="请输入用户名"
            type="text"
            autocomplete="off"
            @keyup.enter.native="handleLogin"
          />
        </div>
        <div class="login-con-box-item">
          <i class="lcbi-pass" />
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          />
        </div>
        <el-button
          :loading="loading"
          class="btn login-btn"
          @click="manageLogin"
        >
          登 录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        userName: "",
        password: "",
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  computed: {
    ...mapGetters(["userInfo"]),
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    handleLogin(event) {
      if (typeof event === "object" || typeof event === "undefined") {
        if (event.keyCode === 13) {
          this.manageLogin();
        }
      } else {
        this.manageLogin();
      }
    },
    manageLogin() {
      if (this.loginForm.username !== "" && this.loginForm.password !== "") {
        this.disabled = true;
        this.login();
      } else {
        if (this.loginForm.username === "") {
          this.$message({
            message: "请输入用户名",
            type: "error",
            center: true,
          });
        } else if (this.loginForm.password === "") {
          this.$message({
            message: "请输入用密码",
            type: "error",
            center: true,
          });
        }
      }
    },
    async login() {
      try {
        this.loading = true;
        const result = await this.$store.dispatch("user/login", this.loginForm);
        if (result.user && result.user.userName) {
          this.$router.push({
            path: "/dashboard",
          });
        } else {
          this.$message.error("获取用户信息失败，请重新登录！");
        }
      } catch (error) {
        console.log("🚀 ~ login ~ error:", error);
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #047a3d;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-con {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url("~@/assets/img/login-bg.png") no-repeat center top;
  background-size: cover;
  display: flex;
  justify-content: space-around;
  align-items: center;
  .login-con-title {
    position: absolute;
    color: #fff;
    height: 110px;
    display: flex;
    align-items: center;
    top: 0;
    img {
      width: 40px;
      margin-right: 15px;
    }
    span {
      font-size: 40px;
      font-family: PingFangSC, PingFangSel-Semibold;
      font-weight: 800;
      letter-spacing: 5px;
      color: #00bff4;
      text-align: center;
      background: linear-gradient(0deg, #009ff1, #00e8cf);
      background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
  .login-con-logo {
    background: url("~@/assets/img/login-left.png") no-repeat center;
    background-size: cover;
    width: 746px;
    height: 649px;
    margin-top: 60px;
  }
  .login-con-box {
    margin-top: 60px;
    background: url("~@/assets/img/login-right.png") no-repeat;
    background-size: cover;
    width: 596px;
    height: 607px;
    padding: 70px;
    box-sizing: border-box;
    .el-form {
      display: flex;
      flex-direction: column;
      width: 100%;
      border-radius: 15px;
      background: rgba(29, 66, 154, 0.2);
      padding: 60px 30px;
      h2 {
        color: #fff;
        font-size: 28px;
        font-family: AlibabaPuHuiTi;
        font-weight: bold;
        text-align: center;
        margin-bottom: 16px;
      }
      .login-con-box-item {
        width: 100%;
        height: 49px;
        margin: 16px auto 0 auto;
        box-sizing: border-box;
        position: relative;
        display: flex;
        i {
          width: 22px;
          height: 22px;
          vertical-align: middle;
          position: absolute;
          top: 14px;
          left: 14px;
        }
        img {
          cursor: pointer;
          width: 110px;
          border: 1px solid #3786fd;
          border-left: none;
        }
        .lcbi-user {
          background: url("~@/assets/img/icon-people.png") no-repeat;
          background-size: cover;
        }
        .lcbi-pass {
          background: url("~@/assets/img/icon-lock2.png") no-repeat;
          background-size: cover;
        }
        .lcbi-code {
          background: url("~@/assets/img/icon-code.png") no-repeat;
          background-size: cover;
        }

        :-webkit-autofill {
          -webkit-text-fill-color: #fff !important;
          transition: background-color 5000s ease-in-out 0s;
        }

        .el-input {
          border: 1px solid #3786fd;
          background: rgba(11, 161, 248, 0.4);
          height: 49px;
          line-height: 49px;
          caret-color: #fff;
          color: #fff;
          width: 100%;
          .el-input__inner {
            border: none;
            background: none;
            outline: none;
            border-radius: 0;
            height: 49px;
            padding-left: 45px;
            font-size: 14px;
            font-family: MicrosoftYaHei-Bold;
            line-height: 49px;
            color: #fff;
            opacity: 1;
          }
        }
        .lcbi-input-width {
          width: 286px;
        }
      }
      .el-button {
        margin-top: 50px;
        border: none;
        width: 100%;
        height: 49px !important;
        background: #3786fd;
        font-weight: bold;
        color: #fff;
        > span {
          font-size: 22px;
          font-family: PingFangSC, PingFangSel-Semibold;
        }
      }
    }
  }
}
</style>
